Partial Class Admin_System_Users
  Inherits System.Web.UI.Page

    Private Sub SecurePage()
        Dim sec As New Secure

        sec.ParentPage = Me
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 3, Secure.ControlTypes.DeleteType)
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 4, Secure.ControlTypes.EditType)
        sec.SetPageAccess(Session("UserName"))
        sec = Nothing

    End Sub

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If DataGrid.Items.Count < 1 Then
      Session.Remove("search_query")
      Search()
        End If

        Dim Edit_Image As Image = CType(Me.Master.FindControl("U1"), Image)
        Edit_Image.ImageUrl = "~/images/redT.PNG"
        Dim Edit_Button As LinkButton = CType(Me.Master.FindControl("System_Users_btn"), LinkButton)
        Edit_Button.ForeColor = Drawing.Color.Black
        Dim Edit_Panel As Panel = CType(Me.Master.FindControl("Users_Panel"), Panel)
    Edit_Panel.Visible = True

    SecurePage()

  End Sub

    Protected Sub Search_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Search_btn.Click
        Call Search()
    End Sub

    Private Sub Search()
        Dim zClass As New LookUp.General_Lookup
        Dim dm As New DAOMain.CommonDAO
        Dim sqlSTR, frmSTR, whereSTR, tempSTR As String

        frmSTR = "" : whereSTR = "" : sqlSTR = "" : tempSTR = ""

        If Trim(Username_txt.Text) <> "" Then
            tempSTR = dm.Q(Trim(Username_txt.Text))
            sqlSTR = sqlSTR & " AND (user_name LIKE '%" & Mid(tempSTR, 2, Len(tempSTR) - 2) & "%')"
        End If

        If Trim(FirstName_txt.Text) <> "" Then
            tempSTR = dm.Q(Trim(FirstName_txt.Text))
            sqlSTR = sqlSTR & " AND (user_first_name LIKE '%" & Mid(tempSTR, 2, Len(tempSTR) - 2) & "%')"
        End If

        If Trim(LastName_txt.Text) <> "" Then
            tempSTR = dm.Q(Trim(LastName_txt.Text))
            sqlSTR = sqlSTR & " AND (user_last_name LIKE '%" & Mid(tempSTR, 2, Len(tempSTR) - 2) & "%')"
        End If

        If Trim(MiddleName_txt.Text) <> "" Then
            tempSTR = dm.Q(Trim(MiddleName_txt.Text))
            sqlSTR = sqlSTR & " AND (user_middle_name LIKE '%" & Mid(tempSTR, 2, Len(tempSTR) - 2) & "%')"
        End If

        If Trim(Department_txt.Text) <> "" Then
            tempSTR = dm.Q(Trim(Department_txt.Text))
            sqlSTR = sqlSTR & " AND (D.lookup_desc LIKE '%" & Mid(tempSTR, 2, Len(tempSTR) - 2) & "%')"
        End If

        If Trim(Role_txt.Text) <> "" Then
            tempSTR = dm.Q(Trim(Role_txt.Text))
            sqlSTR = sqlSTR & " AND (role_desc LIKE '%" & Mid(tempSTR, 2, Len(tempSTR) - 2) & "%')"
        End If

        If Trim(Level_txt.Text) <> "" Then
            tempSTR = dm.Q(Trim(Level_txt.Text))
            sqlSTR = sqlSTR & " AND (L.lookup_desc LIKE '%" & Mid(tempSTR, 2, Len(tempSTR) - 2) & "%')"
            frmSTR = ", [lookup_mstr] L"
            whereSTR = " AND (user_mstr.advisor_level_cd=L.lookup_seq) AND (L.lookup_cd='ADVISOR_LEVEL')"
        End If

        If Advisor_chk.Checked = True Then
            sqlSTR = sqlSTR & " AND (advisor_tag=" & CInt(Advisor_chk.Checked) & ")"
        End If

        Session("search_query") = "FROM [user_mstr], [role_mstr], [lookup_mstr] D" & frmSTR & _
          " WHERE D.[lookup_cd]='DEPARTMENT' AND [role_mstr].[dept_cd]=D.[lookup_seq]" & _
          " AND [user_mstr].[role_cd]=[role_mstr].[role_cd] AND [user_mstr].[dept_cd]=[role_mstr].[dept_cd]" & _
          whereSTR & sqlSTR
        zClass.LookUpCode = "USER_SEARCH"
        DataGrid.CurrentPageIndex = 0
        zClass.DataGridFill(DataGrid, Results_lbl, "", Session("search_query").ToString)
    End Sub

  Protected Sub DataGrid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.ItemCommand
        If e.CommandName = "Edit" Or e.CommandName = "View" Then
      Session("user_cd") = e.Item.Cells(0).Text
      Session("user_mode") = e.CommandName
            Response.Redirect("~/Admin/Add_User.aspx")

        ElseIf e.CommandName = "Delete" Then
            Dim zClass As New LookUp.General_Lookup
            zClass.LookUpCode = "USER_SEARCH"

            DataGrid.CurrentPageIndex = 0
            If zClass.OKtoDELETE(e.Item.Cells(0).Text) = True Then
                zClass.ExecQuery("DELETE FROM user_mstr WHERE user_name='" & e.Item.Cells(0).Text & "'")

                zClass.DataGridFill(DataGrid, Results_lbl, "", Session("search_query").ToString)

            End If

        ElseIf e.CommandName = "Reset" Then

            Try
                Dim polyMisc As New PolyFunc.MiscFunc
                Dim pwd As String
                'Dim pwd, pwd_sec As String
                pwd = UCase(polyMisc.RandomID(6))
                'Dim polySec As New PolyFunc.Secure
                'pwd_sec = polySec.EncodeBase64(pwd)

                Dim sConn As New Data.SqlClient.SqlConnection
                Dim sCom As New Data.SqlClient.SqlCommand
                sConn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("CnnStr")
                sCom.CommandType = Data.CommandType.Text
                sCom.Connection = sConn

                sCom.CommandText = "update user_mstr set " & _
                 "user_password='" & pwd & "' where user_name='" & e.Item.Cells(0).Text & "'"

                sConn.Open()
                sCom.ExecuteNonQuery()
                sConn.Close()

                Dim objConfig As Object
                objConfig = CreateObject("CDO.Configuration")

                With objConfig.Fields
                    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
                    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "202.81.162.37"
                    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
                    .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
                    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
                    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "cims@rtamsi.com"
                    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "janispagal"
                    .Update()
                End With

                Dim objMessage As Object
                Dim dm As New DAOMain.CommonDAO
                objMessage = CreateObject("CDO.Message")
                objMessage.Configuration = objConfig
                objMessage.Subject = "CIMS generated email"
                objMessage.From = """CIMS Administrator"" <CIMSadmin@cims.com>"
                objMessage.To = Trim(e.Item.Cells(8).Text)
                objMessage.TextBody = "Your password has been reset." & vbCrLf & vbCrLf & _
                  "You may login with the following details." & vbCrLf & _
                  "Username: " & Trim(e.Item.Cells(0).Text) & vbCrLf & _
                  "Password: " & pwd & vbCrLf & vbCrLf & _
                  "Do not reply to this email."
                objMessage.Send()

                Dim jj As New PolyFunc.ASPFunc
                jj.MessageAlert(Me, "An email was sent to the user.")

            Catch ex As Exception

            End Try

        End If
  End Sub

  Protected Sub Add_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Add_btn.Click
    Session("user_mode") = "Add"
    Response.Redirect("~/Admin/Add_User.aspx")

  End Sub

  Protected Sub DataGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid.PageIndexChanged
    DataGrid.CurrentPageIndex = e.NewPageIndex
    Dim zClass As New LookUp.General_Lookup
    zClass.LookUpCode = "USER_SEARCH"
    zClass.DataGridFill(DataGrid, Results_lbl, "", Session("search_query").ToString)

  End Sub

  Protected Sub Advisor_chk_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Advisor_chk.CheckedChanged
    Level_txt.Enabled = Advisor_chk.Checked   
  End Sub
End Class